package com.freeter.modules.controller;

import com.freeter.modules.gjs.entity.MstrItems;
import com.freeter.modules.gjs.service.MstrItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/mstr-items")
@CrossOrigin(origins = "*")
public class MstrItemsController {

    @Autowired
    private MstrItemsService mstrItemsService;

    @GetMapping
    public ResponseEntity<List<MstrItems>> getAllItems() {
        List<MstrItems> items = mstrItemsService.getAllItems();
        return ResponseEntity.ok(items);
    }

    @GetMapping("/{id}")
    public ResponseEntity<MstrItems> getItemById(@PathVariable Long id) {
        MstrItems item = mstrItemsService.getItemById(id);
        if (item != null) {
            return ResponseEntity.ok(item);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/code/{itemCode}")
    public ResponseEntity<MstrItems> getItemByCode(@PathVariable String itemCode) {
        MstrItems item = mstrItemsService.getItemByCode(itemCode);
        if (item != null) {
            return ResponseEntity.ok(item);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/cat00/{cat00Code}")
    public ResponseEntity<List<MstrItems>> getItemsByCat00Code(@PathVariable String cat00Code) {
        List<MstrItems> items = mstrItemsService.getItemsByCat00Code(cat00Code);
        return ResponseEntity.ok(items);
    }

    @GetMapping("/cat01/{cat01Code}")
    public ResponseEntity<List<MstrItems>> getItemsByCat01Code(@PathVariable String cat01Code) {
        List<MstrItems> items = mstrItemsService.getItemsByCat01Code(cat01Code);
        return ResponseEntity.ok(items);
    }

    @GetMapping("/cat02/{cat02Code}")
    public ResponseEntity<List<MstrItems>> getItemsByCat02Code(@PathVariable String cat02Code) {
        List<MstrItems> items = mstrItemsService.getItemsByCat02Code(cat02Code);
        return ResponseEntity.ok(items);
    }

    @GetMapping("/division/{divisionCode}")
    public ResponseEntity<List<MstrItems>> getItemsByDivisionCode(@PathVariable String divisionCode) {
        List<MstrItems> items = mstrItemsService.getItemsByDivisionCode(divisionCode);
        return ResponseEntity.ok(items);
    }

    @GetMapping("/uom/{uomCode}")
    public ResponseEntity<List<MstrItems>> getItemsByUomCode(@PathVariable String uomCode) {
        List<MstrItems> items = mstrItemsService.getItemsByUomCode(uomCode);
        return ResponseEntity.ok(items);
    }

    @PostMapping
    public ResponseEntity<MstrItems> createItem(@RequestBody MstrItems item) {
        MstrItems createdItem = mstrItemsService.createItem(item);
        return ResponseEntity.ok(createdItem);
    }

    @PutMapping("/{id}")
    public ResponseEntity<MstrItems> updateItem(@PathVariable Long id, @RequestBody MstrItems item) {
        try {
            MstrItems updatedItem = mstrItemsService.updateItem(id, item);
            return ResponseEntity.ok(updatedItem);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteItem(@PathVariable Long id) {
        mstrItemsService.deleteItem(id);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/code/{itemCode}")
    public ResponseEntity<Void> deleteItemByCode(@PathVariable String itemCode) {
        mstrItemsService.deleteItemByCode(itemCode);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<List<MstrItems>> getItemsByStatus(@PathVariable String status) {
        List<MstrItems> items = mstrItemsService.getItemsByStatus(status);
        return ResponseEntity.ok(items);
    }
}